package com.jeejio.img.util

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import java.io.File

/**
 * Author: JFangZ
 * Email:janezhang1901@163.com
 * Date: 2021/4/15 11:54
 * Description:Util for load img.
 */
object ImgLoadUtil {
    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:51
     * Description:Load img async.
     */
    fun load(context: Context, model: Any, imgView: ImageView) {
        if (context == null) {
            return
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return
        }
        Glide.with(context.applicationContext).load(model).into(imgView)
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:51
     * Description:Load circle img async.
     */
    fun loadCircle(context: Context, model: Any, imgView: ImageView) {
        if (context == null) {
            return
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return
        }
        Glide.with(context.applicationContext).load(model).circleCrop().into(imgView)
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:51
     * Description:Load img by special options async.
     */
    fun load(context: Context, model: Any, imgView: ImageView, requestOptions: RequestOptions) {
        if (context == null) {
            return
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return
        }
        Glide.with(context.applicationContext).load(model).apply(requestOptions).into(imgView)
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:51
     * Description:Load gif async.
     */
    fun loadGif(context: Context, model: Any, imgView: ImageView) {
        if (context == null) {
            return
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return
        }
        Glide.with(context.applicationContext).asGif().load(model).into(imgView)
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:51
     * Description:Load gif async.
     */
    fun loadGif(context: Context, model: Any, imgView: ImageView, requestOptions: RequestOptions) {
        if (context == null) {
            return
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return
        }
        Glide.with(context.applicationContext).asGif().load(model).apply(requestOptions)
            .into(imgView)
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:52
     * Description:Get img sync.
     */
    fun getDrawable(context: Context, model: Any): Drawable? {
        if (context == null) {
            return null
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return null
        }
        return Glide.with(context.applicationContext).load(model).submit().get()
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:52
     * Description:Get img sync.
     */
    fun getFile(context: Context, model: Any): File? {
        if (context == null) {
            return null
        }
        if (context is Activity && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && context.isDestroyed) {
            return null
        }
        return Glide.with(context.applicationContext).downloadOnly().load(model).submit().get()
    }

    /**
     * Author: JFangZ
     * Email:janezhang1901@163.com
     * Date:2021/4/26 15:52
     * Description:Cancel all load img requests.
     */
    fun cancel(context: Context) {
        Glide.with(context.applicationContext).pauseRequests()
    }
}